package org.dromara.capital.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.admin.domain.bo.AdvanceBillBo;
import org.dromara.admin.domain.vo.AdvanceBillVo;
import org.dromara.admin.service.IAdvanceBillService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资方垫资账单信息
 *
 * @author Lion Li
 * @date 2025-09-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/capital/advanceBill")
public class CapitalAdvanceBillController extends BaseController {

    private final IAdvanceBillService advanceBillService;

    /**
     * 查询垫资账单信息列表
     */
//    @SaCheckPermission("capital:advanceBill:list")
    @GetMapping("/list")
    public TableDataInfo<AdvanceBillVo> list(AdvanceBillBo bo, PageQuery pageQuery) {
        return advanceBillService.queryPageList(bo, pageQuery);
    }

    /**
     * 资方获取自己的垫资账单信息列表
     */
    @GetMapping("/myList")
    public TableDataInfo<AdvanceBillVo> myList(AdvanceBillBo bo, PageQuery pageQuery) {
        Long userId = LoginHelper.getUserId();
        if (userId == null){
            throw new ServiceException("资方用户未登录");
        }
        bo.setCapitalUserId(userId);
        return advanceBillService.queryPageMyList(bo, pageQuery);
    }

    /**
     * 导出垫资账单信息列表
     */
//    @SaCheckPermission("capital:advanceBill:export")
    @Log(title = "垫资账单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AdvanceBillBo bo, HttpServletResponse response) {
        List<AdvanceBillVo> list = advanceBillService.queryList(bo);
        ExcelUtil.exportExcel(list, "垫资账单信息", AdvanceBillVo.class, response);
    }

    /**
     * 获取垫资账单信息详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("capital:advanceBill:query")
    @GetMapping("/{id}")
    public R<AdvanceBillVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(advanceBillService.queryById(id));
    }

    /**
     * 新增垫资账单信息
     */
//    @SaCheckPermission("capital:advanceBill:add")
    @Log(title = "垫资账单信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AdvanceBillBo bo) {
        return toAjax(advanceBillService.insertByBo(bo));
    }

    /**
     * 修改垫资账单信息
     */
//    @SaCheckPermission("capital:advanceBill:edit")
    @Log(title = "垫资账单信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AdvanceBillBo bo) {
        return toAjax(advanceBillService.updateByBo(bo));
    }

    /**
     * 资方通过资方账单进行垫资支付
     */
//    @SaCheckPermission("capital:advanceBill:pay")
    @PutMapping("/capitalPay")
    public R<Void> capitalPay(@Validated @RequestBody AdvanceBillBo bo) {
        Long capitalId = LoginHelper.getUserId();
        if (capitalId == null){
            throw new ServiceException("资方用户未登录");
        }
        return toAjax(advanceBillService.capitalPay(bo));
    }
}
